//
//  BarrageManager.swift
//  V1_vodone
//
//  Created by maliang on 2017/4/22.
//  Copyright © 2017年 Arvinma. All rights reserved.
//

import UIKit

let OnceShow = Int(3)

class BarrageManager: UIView {

    var timer:CADisplayLink?
    var waitShowQueue = [String]()
    var showedQueue = [String]()
    var statusArray = [Bool]()
    var showingBarrageArray = [BarrageView]()
    var rowHeight:CGFloat = 0
    var isRepeat = true
    
    class func barrageManagerView(frame:CGRect,inView:UIView) -> BarrageManager{
    
        let managerView = BarrageManager.init(frame: frame)
        managerView.initConfigue()
        inView.addSubview(managerView)
        return managerView
    }
    
    func initConfigue(){
    
        for _ in 1...OnceShow{
            
            self.statusArray.append(true)
        }
        self.rowHeight = self.height / CGFloat(OnceShow)
    }
    
    fileprivate func openTimer(){
    
        if let _ = self.timer{
            
            return
        }
        self.timer = CADisplayLink.init(target: self, selector: #selector(move))
        self.timer?.add(to: RunLoop.current, forMode: .commonModes)
    }
    
    fileprivate func closeTimer(){
        
    }
    
    func showWithQueue(queue:[String]){
    
        self.waitShowQueue.append(contentsOf: queue)
        self.openTimer()
    }
    
    func move(){
        
        let showRowNo = Int(arc4random() % 3)
        let isCanShow = self.statusArray[showRowNo]
        if isCanShow{
            
            if let text = self.waitShowQueue.first{
                
                self.statusArray[showRowNo] = false
                let barrageView = BarrageView.barrageViewWith(text: text,rowNo:showRowNo)
                let centerY = self.rowHeight / 2.0 + self.rowHeight * CGFloat(showRowNo)
                barrageView.centerY  = centerY
                barrageView.left = self.width + barrageView.offSet()
                self.addSubview(barrageView)
                self.showingBarrageArray.append(barrageView)
                self.waitShowQueue.removeFirst()
                if isRepeat{
                    
                    if self.waitShowQueue.isEmpty{
                        
                        self.waitShowQueue.append(contentsOf: self.showedQueue)
                        self.showedQueue.removeAll()
                    }
                }
            }
        }
        self.updateQueue()
    }
    
    func updateQueue(){
    
        var needRemoveArray = [BarrageView]()
        for barrageView in self.showingBarrageArray{
            
            print(barrageView.speed())
            barrageView.left = barrageView.left - barrageView.speed()
            let distance = abs(barrageView.right - self.width)
            if distance <= barrageView.speed() && !barrageView.isShowComplete{
                
                barrageView.isShowComplete = true
                self.statusArray[barrageView.rowNo] = true
            }
            if barrageView.right <= 0{
                
                barrageView.removeFromSuperview()
                needRemoveArray.append(barrageView)
            }
        }
        if needRemoveArray.count > 0{
            
            for barrageView in needRemoveArray{
                
                if let index = self.showingBarrageArray.index(of: barrageView){
                    
                    self.showedQueue.append(barrageView.text!)
                    self.showingBarrageArray.remove(at: index)
                }
            }
            needRemoveArray.removeAll()
        }
    }
}
